import fs from 'fs';
import path from 'path';
import type { Plugin } from 'vite';

// interface OptionVersion {
//     version: number | string;
// }

export default function (str: string): Plugin {
    let config: { publicDir: string } = { publicDir: '' };

    return {
        name: 'version-update',
        configResolved(resolvedConfig) {
            config = resolvedConfig;
        },
        buildStart() {
            const file = path.join(config.publicDir, 'version.js');
            const content = str;

            if (fs.existsSync(config.publicDir)) {
                fs.writeFileSync(file, content, 'utf8');
            } else {
                fs.mkdirSync(config.publicDir, { recursive: true });
                fs.writeFileSync(file, content, 'utf8');
            }
        },
    };
}
